package and.example.addresscontacts;

import and.example.data.Contact;
import and.example.data.DatabaseConnector;
import and.example.interfaces.IArguments;
import and.example.util.Utils;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class AddEditContact extends Activity implements IArguments {

	EditText name;
	EditText phone;
	EditText email;
	EditText street;
	EditText city;
	ImageButton ib;

	long rowID;
	Bundle data;
	private String pathImg = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		name = (EditText) findViewById(R.id.nameEditText);
		phone = (EditText) findViewById(R.id.phoneEditText);
		email = (EditText) findViewById(R.id.emailEditText);
		street = (EditText) findViewById(R.id.streetEditText);
		city = (EditText) findViewById(R.id.cityEditText);
		ib = (ImageButton) findViewById(R.id.btn_choose_img);

		ib.setImageResource(R.drawable.contact2);

		data = getIntent().getExtras();
		if (data != null) {

			rowID = data.getLong(MainActivity.ROW_ID);
			name.setText(data.getString(COLUMNS[2]));
			phone.setText(data.getString(COLUMNS[3]));
			email.setText(data.getString(COLUMNS[4]));
			street.setText(data.getString(COLUMNS[5]));
			city.setText(data.getString(COLUMNS[6]));

			pathImg = data.getString(COLUMNS[7]);
			Bitmap bmp = Utils.decodeSampledBitmapFromFileImagePath(pathImg,
					80, 60);

			if (bmp != null)
				ib.setImageBitmap(bmp);
			else {
				ib.setImageBitmap(Utils.decodeSampledBitmapFromResource(
						getResources(), R.drawable.contact2, 100, 80));
			}

		}

		RelativeLayout layout = (RelativeLayout) findViewById(R.id.header_layout);
		ImageButton btn = (ImageButton) layout.findViewById(R.id.header_left_btn);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AddEditContact.this.finish();
			}
		});

	}

	public void onclick(View v) {

		if (v.getId() == R.id.saveContactButton) {

			if (name.getText().length() != 0) {

				AsyncTask<Void, Object, Object> saveContactTask = new AsyncTask<Void, Object, Object>() {
					@Override
					protected Object doInBackground(Void... params) {
						saveContact();
						return null;
					}

					@Override
					protected void onPostExecute(Object result) {
						finish();
						super.onPostExecute(result);
					}
				};
				saveContactTask.execute();
			} else {

				Builder bui = new Builder(this);
				bui.setTitle(R.string.errorTitle)
						.setMessage(R.string.errorMessage)
						.setPositiveButton(R.string.errorButton, null).show();
			}
			return;
		}

		if (v.getId() == R.id.btn_choose_img) {

			Intent i = new Intent(Intent.ACTION_PICK);
			i.setType("image/*");
			startActivityForResult(i, 200);
			return;
		}

		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == 200 && resultCode == RESULT_OK) {

			try {

				Uri uri = data.getData();
				Cursor c = getContentResolver().query(uri,
						new String[] { MediaStore.Images.Media.DATA }, null,
						null, null);
				c.moveToFirst();
				pathImg = c.getString(c
						.getColumnIndex(MediaStore.Images.Media.DATA));

				Bitmap bmp = Utils.decodeSampledBitmapFromFileImagePath(
						pathImg, 60, 40);

				// /Bitmap bitmap =
				// Utils.createScaledBitmapKeepingAspectRatio(bmp, 150);
				ib.setImageBitmap(bmp);

			} catch (Exception e) {
				Log.e("Loi ham Edit anh==>", "Xem tu day", e);
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			}

		}
	}

	protected void saveContact() {

		Contact contact = new Contact(name.getText().toString(), phone
				.getText().toString(), email.getText().toString(), street
				.getText().toString(), city.getText().toString(), pathImg);
		DatabaseConnector databaseConnector = new DatabaseConnector(this);

		if (data == null) {

			databaseConnector.insertContact(contact);
		} else {

			databaseConnector.updateContact(rowID, contact);
		}

	}

}
